package neu.edu.cn.nuemall.controller;

import com.github.pagehelper.util.StringUtil;
import neu.edu.cn.nuemall.dto.CartItemDTO;
import neu.edu.cn.nuemall.entity.CartItem;
import neu.edu.cn.nuemall.service.CartItemService;
import neu.edu.cn.nuemall.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class CartController {

    @Autowired
    private UserinfoService userinfoService;

    @Autowired
    private CartItemService cartService;

    @RequestMapping("/my-cart")
    public String addToCart(String addCartSkuId, String removeId, Model model){

        String userId=userinfoService.getCurrentUser();
        if (StringUtils.hasText(userId) && StringUtils.hasText(addCartSkuId)){
            CartItem cartItem=new CartItem();
            cartItem.setSkuId(addCartSkuId);
            cartItem.setUserId(userinfoService.getCurrentUser());
            cartItem.setCount(1);

            int i=cartService.addToCart(cartItem);
            model.addAttribute("isAddToCart",true);
        }
        if (StringUtils.hasText(removeId)){

            int i=cartService.deleteCart(removeId);
            model.addAttribute("isRemoveFromCart",true);

        }
        List<CartItemDTO> cartItemDTOList=cartService.getCartItemList();
        model.addAttribute("cartItemList",cartItemDTOList);
        return "/cart/cartList";

    }
}
